package com.example.test_activity.utils;

import android.content.Context;

import com.example.test_activity.entity.Syllabus;
import com.example.test_activity.entity.Week;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 旭 on 2017/3/16/016.
 */

public class GetSyllabus {

//    private final String url = "http://jwxt.cisau.com.cn:9091/xkAction.do?actionType=6";
    private final String url = "http://jwxt.cisau.com.cn:9090/xkAction.do?actionType=6";
//    private final String url = "http://jwxt.sxau.edu.cn/xkAction.do?actionType=6";


    private List<Map<String,String>> list = new ArrayList<>();
    private List<Map<String,String>> list_find = new ArrayList<>();

    public List<Map<String,String>> getWeek(String html)
    {

            Document document = Jsoup.parse(html);
            Elements elements = document.select(".pageAlign");
            if (elements.isEmpty())
            {
                return list;
            }
            Elements elements_3 = elements.get(1).getAllElements().select(".odd");
            int k=0;
            for (Element element : elements_3)
            {
                Week week = new Week();
                for (int i = 0;i<element.children().size();++i)
                {
                    Element ele = element.child(i);
                    if (element.children().size() == 7)
                    {
                        switch (i)
                        {
                            case 0:
                                week.setWeek(ele.text());
                                break;
                            case 1:
                                week.setWeekend(ele.text());
                                break;
                            case 2:
                                week.setNum(ele.text());
                                break;
                            case 4:
                                week.setSchool(ele.text());
                                break;
                            case 5:
                                week.setOne(ele.text());
                                break;
                            case 6:
                                week.setTwo(ele.text());
                                break;
                        }
                    }else {
                        switch (i)
                        {
                            case 2:
                                week.setName(ele.text());
                                break;
                            case 3:
                                week.setCno(ele.text());
                            case 11:
                                week.setWeek(ele.text());
                                break;
                            case 12:
                                week.setWeekend(ele.text());
                                break;
                            case 13:
                                week.setNum(ele.text());
                                break;
                            case 15:
                                week.setSchool(ele.text());
                                break;
                            case 16:
                                week.setOne(ele.text());
                                break;
                            case 17:
                                week.setTwo(ele.text());
                                break;
                        }
                    }
                }
                ++k;
                getInfo(week);
            }

        for (int i=0;i<list_find.size();++i)
        {
            Map<String,String> map = list_find.get(i);
            for (String key : map.keySet())
            {
                if (map.get(key) == null)
                {
                    map.put(key,list_find.get(i-1).get(key));
                }
            }
        }

        return list_find;
    }


    private void getInfo(Week week)
    {
        Map<String,String> map = new HashMap<>();
        map.put("name",week.getName());
        map.put("week",week.getWeek());
        map.put("weekend",week.getWeekend());
        map.put("num",week.getNum());
        map.put("max","16");
        map.put("min","1");
        map.put("flag","2");
        map.put("cno",week.getCno());
        map.put("school",week.getSchool());
        map.put("one",week.getOne());
        map.put("two",week.getTwo());

        list_find.add(map);
    }
}
